package com.han.consumer2;

import com.netflix.loadbalancer.IRule;
import com.netflix.loadbalancer.WeightedResponseTimeRule;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.client.RestTemplate;

@EnableEurekaClient
@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)
@CrossOrigin
@Controller
public class Consumer2Application {

    public static void main(String[] args) {
        SpringApplication.run(Consumer2Application.class, args);
    }


    //设置负载均衡其实在调用别人的时候才有用
    @Bean
    public IRule ribbonRule() {
        // 负载均衡规则改为随机(注意默认为轮询)
        return new WeightedResponseTimeRule();
    }
    @Bean
    @LoadBalanced
        //表示RestTemplate开启了负载均衡,主要用于集群负载,基本只有消费者才用
        //注意默认负载均衡是轮训,就是用完这个provider再用另一个
    RestTemplate rest() {
        return new RestTemplate();
    }


    @GetMapping("/f1")
    @ResponseBody
    public String f(){
        return "调用consumer的f2成功222";
    }
}
